package cn.com.woxitech.dev.controller.api;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import cn.com.woxitech.dev.common.APIResultFactory;
import cn.com.woxitech.dev.common.Const;
import cn.com.woxitech.dev.common.HttpClientUtils;
import cn.com.woxitech.dev.common.JSONUtils;
import cn.com.woxitech.dev.common.QiniuPhotoUploadUtils;
import cn.com.woxitech.dev.model.Lecture;
import cn.com.woxitech.dev.model.QiniuUploadResult;
import cn.com.woxitech.dev.model.RoomManager;
import cn.com.woxitech.dev.model.WmChathistoryFetch;
import cn.com.woxitech.dev.model.gen.WmApp;
import cn.com.woxitech.dev.model.gen.WmLecture;
import cn.com.woxitech.dev.model.gen.WmLecturePage;
import cn.com.woxitech.dev.model.gen.WmRoom;
import cn.com.woxitech.dev.model.gen.WmUser;
import cn.com.woxitech.dev.service.AppService;
import cn.com.woxitech.dev.service.CatalogService;
import cn.com.woxitech.dev.service.LecturePageService;
import cn.com.woxitech.dev.service.LectureService;
import cn.com.woxitech.dev.service.OrganizationService;
import cn.com.woxitech.dev.service.QiniuService;
import cn.com.woxitech.dev.service.RoomService;
import cn.com.woxitech.dev.service.SMSCodeService;
import cn.com.woxitech.dev.service.UserService;

@RestController
@RequestMapping("/luyan")
public class UserLUYANController {
	
	@Autowired
	QiniuService qiniuService;
	@Autowired
	RoomService roomService;
	@Autowired
	LectureService lectureService;
	@Autowired
	LecturePageService lecturePageService;
	@Autowired
	UserService userService;
	@Autowired
	SMSCodeService smsCodeService;
	@Autowired
	OrganizationService organizationService;
	@Autowired
	CatalogService catalogService;	
	@Autowired
	AppService appService;
	
	@RequestMapping(value = "/user/{userid}/followinfo",method=RequestMethod.POST)
	public Object user_followinfo(@PathVariable String userid, @RequestBody String body){
		Map<String, Object> mp = new HashMap<String, Object>();
		
		try {
//			JsonNode jsonNode = JSONUtils.toJson(body);
			
			mp.put("followingCount", this.userService.getFollowingCount(userid));
			mp.put("followerCount", this.userService.getFollowerCount(userid));
			
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
	}	
	
	@RequestMapping(value = "/user/{userid}/update",method=RequestMethod.POST)
	public Object user_updateinfo(@PathVariable String userid, @RequestBody String body){
		Map<String, Object> mp = new HashMap<String, Object>();
		
		try {
			JsonNode jsonNode = JSONUtils.toJson(body);
			WmUser user = this.userService.getUserByUuid(userid);
			if(user == null){
				return APIResultFactory.success(false);
			}
			
			String tel = jsonNode.get("tel").asText();
			
			user.setTel(tel);
			this.userService.updateUser(user);
			
			return APIResultFactory.success(true);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
	}	
	

	@RequestMapping(value = "/user/{userid}",method=RequestMethod.POST)
	public Object user_info(@PathVariable String userid, @RequestBody String body){
		Map<String, Object> mp = new HashMap<String, Object>();
		
		try {
			JsonNode jsonNode = JSONUtils.toJson(body);
			WmUser user = this.userService.getUserByUuid(userid);
			if(user == null){
				mp.put("next", false);
			}else{
				mp.put("headimgurl", user.getWxHeadimgurl());
				mp.put("name", user.getName());
				mp.put("unionid", user.getWxUnionid());
				mp.put("useruuid", user.getUuid());
				mp.put("tel", user.getTel());
				mp.put("usertype", user.getUserType());
				mp.put("next", true);
			}

			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
	}	
	
	@RequestMapping(value = "/users",method=RequestMethod.POST)
	public Object users(@RequestBody String body){
		Map<String, Object> mp = new HashMap<String, Object>();
		
		try {
			JsonNode jsonNode = JSONUtils.toJson(body);
			String usertype = null;
			int size = 20;
			int pos = 0;
			String refreshtype = "head";//footer
			String tel = null;
			if(jsonNode.has("usertype")){
				usertype = jsonNode.get("usertype").asText();
			}
			if(jsonNode.has("size")){
				size = jsonNode.get("size").asInt();
			}
			if(jsonNode.has("pos")){
				pos = jsonNode.get("pos").asInt();
			}
			if(jsonNode.has("refreshtype")){
				refreshtype = jsonNode.get("refreshtype").asText();
			}
			if(jsonNode.has("tel")){
				tel = jsonNode.get("tel").asText();
			}
			List<WmUser> users = this.userService.getUsersByUsertype(usertype,size,pos,refreshtype,tel);
			mp.put("users", users);
			mp.put("pos", pos + users.size());
			return APIResultFactory.success(mp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
	}	

	@RequestMapping(value="/users/online",method=RequestMethod.POST)
	public Object init_online(@RequestBody String body){
		
		try {
			ArrayNode an = JSONUtils.arrayInit();
			JsonNode jsonNode = JSONUtils.toJson(body);
			String roomuuid = jsonNode.get("roomuuid").asText();
        	List<WmUser> users = this.roomService.getOnlineUser(roomuuid, 10);
        	
        	if(users != null){
	        	for(WmUser user : users){
	            	ObjectNode nc_temp = JSONUtils.jsonInit();
	            	nc_temp.put("headimgurl", user.getWxHeadimgurl());
	            	nc_temp.put("useruuid", user.getUuid());
	        		nc_temp.put("name", user.getName());
	        		nc_temp.put("sessionid", System.currentTimeMillis());
	        		an.add(nc_temp);
	        	}
        	}

			return APIResultFactory.success(an);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return APIResultFactory.fail();
		}
		
	}
	
}
